package com.jspgou.cms.dao.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.springframework.stereotype.Repository;

import com.jspgou.cms.dao.BalanceDao;
import com.jspgou.cms.entity.Balance;
import com.jspgou.common.hibernate3.Finder;
import com.jspgou.common.hibernate3.HibernateBaseDao;
import com.jspgou.common.page.Pagination;

@Repository
public class BalanceDaoImpl extends HibernateBaseDao<Balance, Long> implements BalanceDao {
	public Pagination getPage(int pageNo, int pageSize) {
		Criteria crit = createCriteria();
		Pagination page = findByCriteria(crit, pageNo, pageSize);
		return page;
	}
	
	public Pagination getPage(Long storeId,String storeName,Date startTime,
			Date endTime,int pageNo, int pageSize){
		Finder f=Finder.create("from Balance bean where 1=1 ");
		if(storeId!=null){
			f.append(" and bean.store.id=:storeId");
			f.setParam("storeId", storeId);
		}
		if(!StringUtils.isBlank(storeName)){
			f.append(" and bean.store.storeExt.storeName like:storeName");
			f.setParam("storeName", "%"+storeName+"%");
		}
		if(startTime!=null){
			f.append(" and bean.startTime>:startTime");
			f.setParam("startTime", startTime);
		}
		if(endTime!=null){
			f.append(" and bean.endTime<:endTime");
			f.setParam("endTime", endTime);
		}
		return find(f,pageNo,pageSize);
	}
	
	public List<Balance> getList(String storeName,Date startTime,Date endTime){
		Finder f=Finder.create("from Balance bean where 1=1 ");
		if(!StringUtils.isBlank(storeName)){
			f.append(" and bean.store.storeName like:storeName");
			f.setParam("storeName", "%"+storeName+"%");
		}
		if(startTime!=null){
			f.append(" and bean.startTime>:startTime");
			f.setParam("startTime", startTime);
		}
		if(endTime!=null){
			f.append(" and bean.endTime<:endTime");
			f.setParam("endTime", endTime);
		}
		return find(f);
	}

	public Balance findById(Long id) {
		Balance entity = get(id);
		return entity;
	}

	public Balance save(Balance bean) {
		getSession().save(bean);
		return bean;
	}

	public Balance deleteById(Long id) {
		Balance entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}
	
	@Override
	protected Class<Balance> getEntityClass() {
		return Balance.class;
	}
}